<?php
/**
 * Template Name: Message Center
 *
 * @package wanzicms
 */

get_header();
?>

<div id="primary" class="content-area flex-grow">
    <main id="main" class="site-main container mx-auto pt-24 pb-8 px-4 sm:px-6 lg:px-8">

        <header class="page-header mb-6">
            <h1 class="text-3xl font-bold leading-tight text-gray-900"><?php the_title(); ?></h1>
        </header>

        <div>
            <div class="border-b border-gray-200">
                <nav class="-mb-px flex space-x-8" aria-label="Tabs">
                    <a href="#system-messages" class="tab-link whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm border-indigo-500 text-indigo-600">
                        系统消息
                    </a>
                    <a href="#comment-notifications" class="tab-link whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300">
                        评论通知
                    </a>
                </nav>
            </div>

            <div id="system-messages" class="tab-content py-6">
                <?php
                $args = array(
                    'post_type' => 'system_message',
                    'posts_per_page' => -1,
                    'orderby' => 'date',
                    'order' => 'DESC',
                );

                $system_messages = new WP_Query( $args );

                if ( $system_messages->have_posts() ) :
                    echo '<div class="space-y-4">';
                    while ( $system_messages->have_posts() ) : $system_messages->the_post();
                        ?>
                        <div class="bg-white shadow-sm rounded-lg p-6">
                            <h3 class="text-lg font-semibold text-gray-900"><?php the_title(); ?></h3>
                            <div class="text-sm text-gray-500 mt-1"><?php echo get_the_date(); ?></div>
                            <div class="prose max-w-none mt-2 text-gray-700"><?php the_content(); ?></div>
                        </div>
                        <?php
                    endwhile;
                    echo '</div>';
                    wp_reset_postdata();
                else :
                    echo '<p class="text-gray-500">当前没有系统消息。</p>';
                endif;
                ?>
            </div>

            <div id="comment-notifications" class="tab-content hidden py-6">
                <?php
                if ( is_user_logged_in() ) :
                    $current_user_id = get_current_user_id();
                    $notifications = get_user_meta( $current_user_id, 'wz_notifications' );

                    if ( ! empty( $notifications ) ) :
                        echo '<ul class="divide-y divide-gray-200">';
                        $notifications = array_reverse( $notifications );

                        foreach ( $notifications as $notification ) {
                            $commenter_name = '';
                            if ( !empty($notification['comment_author_id']) && $notification['comment_author_id'] > 0 ) {
                                $commenter = get_user_by( 'id', $notification['comment_author_id'] );
                                $commenter_name = $commenter ? $commenter->display_name : '';
                            }
                            
                            if (empty($commenter_name)) {
                                $comment_obj = get_comment( $notification['comment_id'] );
                                $commenter_name = $comment_obj ? $comment_obj->comment_author : __('一位访客', 'wanzicms');
                            }

                            $post_title = get_the_title( $notification['post_id'] );
                            $post_url = get_permalink( $notification['post_id'] );
                            $notification_text = '';

                            if ( $post_url ) {
                                if ( 'post_reply' === $notification['type'] ) {
                                    $notification_text = sprintf(
                                        '%s 在您的文章 <a href="%s" class="font-medium text-indigo-600 hover:text-indigo-500">%s</a> 中回复了您。',
                                        $commenter_name,
                                        esc_url( $post_url . '#comment-' . $notification['comment_id'] ),
                                        $post_title
                                    );
                                } elseif ( 'comment_reply' === $notification['type'] ) {
                                    $notification_text = sprintf(
                                        '%s 在文章 <a href="%s" class="font-medium text-indigo-600 hover:text-indigo-500">%s</a> 中回复了您的评论。',
                                        $commenter_name,
                                        esc_url( $post_url . '#comment-' . $notification['comment_id'] ),
                                        $post_title
                                    );
                                }
                            } else {
                                if ( 'post_reply' === $notification['type'] ) {
                                     $notification_text = sprintf('%s 在一篇文章中回复了您 (文章可能已被删除)。', $commenter_name);
                                } elseif ( 'comment_reply' === $notification['type'] ) {
                                     $notification_text = sprintf('%s 回复了您的一条评论 (文章可能已被删除)。', $commenter_name);
                                }
                            }
                            
                            $read_class = $notification['read'] ? 'text-gray-600' : 'text-gray-900 font-semibold';
                            echo "<li class='py-4 {$read_class}'>{$notification_text} <span class='block text-sm text-gray-500 font-normal pt-1'>" . date_i18n( 'Y-m-d H:i', $notification['timestamp'] ) . "</span></li>";
                        }
                        echo '</ul>';

                        // Mark all as read
                        $unread_notifications = get_user_meta( $current_user_id, 'wz_notifications', false );
                        delete_user_meta($current_user_id, 'wz_notifications');
                        foreach($unread_notifications as $notification) {
                            $notification['read'] = true;
                            add_user_meta($current_user_id, 'wz_notifications', $notification, false);
                        }

                    else :
                        echo '<p class="text-gray-500">您没有新的评论通知。</p>';
                    endif;

                else :
                    echo '<p class="text-gray-500">请 <a href="' . wp_login_url( get_permalink() ) . '" class="text-indigo-600 hover:text-indigo-900">登录</a> 查看您的通知。</p>';
                endif;
                ?>
            </div>
        </div>
    </main>
</div>

<?php
get_footer(); 